#include "game.h"
#include "ui_game.h"
#include"mainwindow.h"
game::game(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::game)
{
    ui->setupUi(this);
    this->setFixedSize(1480,1280);
    for(int i = 0; i < 9;i++)
    {
        for(int j =0 ; j < 10; j++)
        {
            zi[i][j] = 0;
        }
    }
}

game::~game()
{
    delete ui;
}
void  game::paintEvent(QPaintEvent *)
{

  paint=new QPainter;
  paint->begin(this);

  paint->setPen(QPen(Qt::darkGreen,2,Qt::SolidLine));

  for(int h=0;h<3;h++)
  {
      int y1=y+WIDTH*4*h;
      for(int j=0;j<3;j++)
  {
      int x1=x+WIDTH*4*j;
      for(int i=0;i<4;i++)
      {
          paint->drawLine(x1,y1+WIDTH*i,x1+WIDTH*3,y1+WIDTH*i);
      }

      for(int i=0;i<4;i++)
      {
          paint->drawLine(x1+WIDTH*i,y1,x1+WIDTH*i,y1+WIDTH*3);
      }

  }
  }
  if(soon!=-1)
  {
      paint->setPen(QPen(Qt::blue,2,Qt::SolidLine));
      int kk=soon/3;
      int pp=soon%3;
      int y1=y+WIDTH*4*kk;
      int x1=x+WIDTH*4*pp;
      for(int j=0;j<3;j++)
      {
      for(int i=0;i<4;i++)
      {
          paint->drawLine(x1,y1+WIDTH*i,x1+WIDTH*3,y1+WIDTH*i);
      }

      for(int i=0;i<4;i++)
      {
          paint->drawLine(x1+WIDTH*i,y1,x1+WIDTH*i,y1+WIDTH*3);
      }
      }
  }
   for(int i=0;i<9;i++)
   {
       for(int j=0;j<9;j++)
       {
           int x1=x+i%3*4*WIDTH+j%3*WIDTH;
           int y1=y+i/3*4*WIDTH+j/3*WIDTH;
           if(zi[i][j]==1){
               paint->setBrush(QBrush(Qt::black,Qt::SolidPattern));
               paint->drawEllipse(x1+25,y1+25,WIDTH/2,WIDTH/2);}
           if(zi[i][j]==-1){
               paint->setBrush(QBrush(Qt::green,Qt::SolidPattern));
               paint->drawEllipse(x1+25,y1+25,WIDTH/2,WIDTH/2);}
       }
   }

}
void game::mousePressEvent(QMouseEvent *event)
{
    QPoint pos = event->pos();
       for(int i = 0; i < 9;i++)
       {
           for(int j =0 ; j < 9; j++)
           {
               QRect rect = QRect(x+i%3*4*WIDTH+j%3*WIDTH, y+i/3*4*WIDTH+j/3*WIDTH, WIDTH,WIDTH);
               if(rect.contains(pos))
               {
                   if(i==soon||soon==-1)
                   if(event->button() == Qt::LeftButton&&zi[i][j] == 0&&zi[i][9]==0)
                   {
                       xia(i,j);
                   }

                   break;
               }
           }
       }
}
void game::xia(int aa,int bb)
{
    copy1();
    zi[aa][bb]=first1;
    first1=(-1)*first1;
    soon=bb;
    judge(zi);
    if(zi[bb][9]!=0)
        soon=-1;
    win();
    if(pva==0)
    {
        int bbb=ai();
        zi[soon][bbb]=first1;
        first1=(-1)*first1;
        soon=bbb;
        judge(zi);
        if(zi[bbb][9]!=0)
            soon=-1;
        win();
    }
    this->update();
}
void game::judge(int a[9][10])
{
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<8;j++)
        {
           int x=three[j][0];
           int y=three[j][1];
           int z=three[j][2];
           if(a[i][x]==a[i][y]&&a[i][j]==a[i][z]&&a[i][x]!=0)
           {
               zi[i][9]=a[i][x];
           }

        }
    }
}
void game::win()
{
    for(int j=0;j<8;j++)
    {
       int x=three[j][0];
       int y=three[j][1];
       int z=three[j][2];
       if(zi[x][9]==zi[y][9]&&zi[x][9]==zi[z][9]&&zi[x][9]!=0)
       {
           if(zi[x][9]==1)
               nowwin=1;
           else
               nowwin=-1;
       }
    }

}
void game::copy()
{
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<10;j++)
        {
            zi1[i][j]=zi[i][j];
        }
    }
}
void game::copy1()
{
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<10;j++)
        {
            back[i][j]=zi[i][j];
            last=soon;
        }
    }
}
void game::copy2()
{
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<10;j++)
        {
            zi[i][j]=back[i][j];
            soon=last;
        }
    }
}
int game::ai()
{

    int xfen[9]={0};
    int r1;
    int gfen=0;
    if(soon==-1)
    {
       int yfen[9]={0};
       int gfen2=0;
       copy();
       for(int i=0;i<9;i++)
       {

           if(zi1[i][9]==0)
           {
               zi1[i][9]=first1;
               yfen[i]=score2(i);
               zi1[i][9]=0;
           }
       }
       for(int i=0;i<9;i++)
       {
           if(yfen[i]>=gfen2)
           {
               gfen2=yfen[i];
               soon=i;
           }
       }
    }
    if(soon!=-1){copy();
    for(int i=0;i<9;i++)
    {
        if(zi1[soon][i]==0)
        {
            int soon1=soon;
            zi1[soon][i]=first1;
            xfen[i]=score(i);
            zi1[soon][i]=0;
            zi1[soon][9]=first1;
            xfen[i]*=score2(soon);
            zi1[soon][9]=0;
            zi1[soon][i]=first1;
            first1=(-1)*first1;
            soon=i;
            judge(zi1);
            if(zi[soon][9]!=0)
                soon=-1;
            xfen[i]-=ai2();
            first1=(-1)*first1;
            soon=soon1;
            zi1[soon][i]=0;
        }
    }
    for(int i=0;i<9;i++)
    {
        if(xfen[i]>=gfen)
        {
            gfen=xfen[i];
            r1=i;
        }
    }
    }
    return r1;
}
int game::ai2()
{
    int xfen[9]={0};
    int r1;
    int gfen=0;
    if(soon==-1)
    {
       int yfen[9]={0};
       int gfen2=0;
       for(int i=0;i<9;i++)
       {

           if(zi1[i][9]==0)
           {
               zi1[i][9]=first1;
               yfen[i]=score2(i);
               zi1[i][9]=0;
           }
       }
       for(int i=0;i<9;i++)
       {
           if(yfen[i]>=gfen2)
           {
               gfen2=yfen[i];
               soon=i;
           }
       }
    }
    if(soon!=-1){copy();
    for(int i=0;i<9;i++)
    {
        if(zi1[soon][i]==0)
        {
            zi1[soon][i]=first1;
            xfen[i]=score(i);
            zi1[soon][i]=0;
            zi1[soon][9]=first1;
            xfen[i]*=score2(soon);
            zi1[soon][9]=0;


        }
    }
    for(int i=0;i<9;i++)
    {
        if(xfen[i]>=gfen)
        {
            gfen=xfen[i];

        }
    }
    }
    return gfen;
}
int game::score(int t)
{
    int max=0;
    for(int j=0;j<8;j++)
    {
       int x=three[j][0];
       int y=three[j][1];
       int z=three[j][2];
       if(t==x||t==y||t==z)
       {
           int the=zi1[soon][x]+zi1[soon][y]+zi1[soon][z];
           int tt=zi1[soon][t];
           if(the==3*tt)
           {
               max=1000;
               return max;
           }
           if(the==(-1)*tt)
               max+=100;
           if(the==2*tt)
               max+=50;
           if(the==0)
               max+=2;
           if(the==tt)
               max+=3;
       }
     }
    return max;
}
int game::score2(int t)
{
    int max;
    for(int j=0;j<8;j++)
    {
       int x=three[j][0];
       int y=three[j][1];
       int z=three[j][2];
       if(t==x||t==y||t==z)
       {
           int the=zi1[x][9]+zi1[y][9]+zi1[z][9];
           int tt=zi1[t][9];
           if(the==3*tt)
           {
               max=1000;
               return max;
           }
           if(the==(-1)*tt)
               max+=100;
           if(the==2*tt)
               max+=50;
           if(the==0)
               max+=2;
           if(the==tt)
               max+=3;
       }
     }
    return max;
}
void game::winned()
{
    int ret=QMessageBox::information(this, "你输了", "退出?", QMessageBox::Yes | QMessageBox::No);
    if(ret==QMessageBox::Yes){
        for(int i = 0; i < 9;i++)
        {
            for(int j =0 ; j < 10; j++)
            {
                zi[i][j] = 0;
            }
             this->update();
        }

    }
    else
        this->close();
}
void game::on_pushButton_clicked()
{
    copy2();
    this->update();
}
void game::closeEvent(QCloseEvent *event)
{
    int ret=QMessageBox::information(this, "退出", "是否要退出?", QMessageBox::Yes | QMessageBox::No);
    if(ret==QMessageBox::Yes){
        qApp->closeAllWindows();
        qApp->quit();
        event->accept();
    }
    else
        event->ignore();
}
void game::sleep()
{
QTime dieTime = QTime::currentTime().addMSecs(500);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

void game::on_pushButton_3_clicked()
{
    emit sendsignal();
    this->close();



}

void game::on_pushButton_2_clicked()
{
    winned();
}
